<?php
include 'class/db/DbProject.php';
/**
 * Description of ProjectList
 *
 * @author GHoogendoorn
 * @version 0.1
 * 
 * Version history
 * 0.1  GHoogendoorn    Initial version
 */
class ProjectList {
    

    private $project_list;
    private $project_id_array;
    private $project_list_pointer;
    
    
    public function __construct($id='') {
        //parent::__construct();
        if ( (strlen($id) > 0 ) && $this->dbCheckId ($id, DB_FIELD_COURSE_ID) ){
            $this->selectById($id);
        }
        $this->loadProjectIDList();
        
        $this->loadProjectList();
        
        $this->resetProjectList();
    }

    
    public function selectById($id=''){
        if ( (strlen($id) < 1) && (!is_numeric($this->project_id)) ){
            
        }
    }
    
    
    /**
     * getProjectList
     * 
     *  Return a listof all projectnames and Ids
     */
    public function getProjectList(){
        
        return $this->project_list;
    }
    
    public function getProjectListIDs(){
        return $this->project_id_array;
    }
    
    
    
    /**
     * getNextProject()
     * 
     * @return FALSE or current project array
     */
    public function getNextProject(){
        
        /**
         * If first time retrieve project list.
         */
        if (!is_array($this->project_list)){
            
            $this->loadProjectList();
            
        }
        
        /*
        echo __FILE__.__LINE__.__FUNCTION__.'<br />';
        echo '<pre>';
        var_dump($this->project_list);
        echo '</pre>';
        //*/
        
        /**
         * Return the current project.
         */
        if (array_key_exists($this->project_list_pointer, $this->project_list)){
            
            $current_pointer = $this->project_list_pointer;
            /**
             * Increase the project pointer 
             */
            $this->project_list_pointer++;
            
            /**
             * return the previous idx of the project 
             */
            return $this->project_list[ $current_pointer ];
        }
        
        /**
         *  Last entry or invalid entry
         * 
         *  Reset the pointer and return FALSE
         */
        $this->resetProjectList();
        
        return false;
        
    }
    
    /**
     * getProjectPersonList()
     * 
     * Retrieve the list with all persons related to this project.
     * 
     */
    public function getProjectPersonList($proj_id){

        // Haal een lijst op om alle Projecten met een projectleider op te halen
        $project_db = new DbProject();
        $project_person_list = $project_db->getProjectPersonList($proj_id);
        
        return $project_person_list;
    }
    
    
    
    /**
     * loadProjectList()
     * 
     *  Retrieve all projects
     *  Store in class project_list variable.
     */
    public function loadProjectList(){
        
        $db = new DbProject();
        $proj_id_array = $db->getProjectList();
        
        echo __FILE__. __LINE__ . __FUNCTION__ .'<br />';
        var_dump( $proj_id_array);
        
        // Sla de lijst op in de klasse variabele
        foreach ( $proj_id_array as $idx => $proj ){
            $this->project_list[] = array('id' => (int)$proj['project_id'], 'name' => $proj['projectnaam']);
        }
        
        
    }
    
    public function resetProjectList(){
        
        $this->project_list_pointer = 0;
    }
    
    /**
     * getProjectList()
     *  Haal alle projectIds uit de database en sla op in de project_id_array
     * 
     */
    private function loadProjectIDList(){
        // Haal een lijst op om alle projecten ook zonder projectleider op te halen
        $db = new DbProject();
        $proj_id_array = $db->getProjectListIds();
        
        // Sla de lijst op in de klasse variabele
        $this->project_id_array = array(); 
        foreach ( $proj_id_array as $idx => $proj_id ){
            $this->project_id_array[] = (int)$proj_id['project_id'];
        }
    }


}

class TestProjectList {

    public function TestProjectList() {
        try {
            $test = new ProjectList();
            
            $cnt = 0;
            while( $person_array = $project_list->getNextProject() ){
                $cnt++;
                echo "<tr><td>".$person_array['id']."</td><td>".$person_array['name']."</td></tr>";
            }
            
            if ($cnt == 0){
                throw new Exception('No project in DB');
            }
            
        } catch (Exception $e) {
            echo '<pre>';
            echo $e->getTraceAsString();
            echo '</pre>';
            trigger_error($e->getMessage());
        }
    }

}

?>
